package com.ctg.itrdc.k8sconsole.service.impl;

import java.net.URL;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.common.util.HttpRequestUtil;
import com.ctg.itrdc.imageconsole.data.dao.IClusterConfigDao;
import com.ctg.itrdc.imageconsole.data.dto.PodQueryDTO;
import com.ctg.itrdc.imageconsole.data.entity.ClusterConfig;
import com.ctg.itrdc.k8sconsole.service.IPodService;
import com.ctg.itrdc.paspsdk.common.utils.file.PropertiesUtil;

/**
 * 
 * @description: pod相关查询
 * @version: v1.0.0
 * @author: Piao.Wu
 * 
 *          Modification History: Date Author Version Description
 *          -----------------------------------------------------------------
 *          2016年8月20日 上午10:39:43 Piao.Wu v1.0.0 create
 *
 */
@Component("podService")
public class PodServiceImpl implements IPodService {

	@Resource
	private IClusterConfigDao clusterConfigDao;

	@Override
	public String getPodsByRc(String clusterCode, String perpage, String page, String namesp, String name)
			throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getPodsByRc.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode, namesp, name);
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = HttpRequestUtil.target;
		Response response = target.queryParam("perpage", perpage).queryParam("page", page).request().get();

		return requestUtil.getResponseJsonData(response);
	}

	@Override
	public String getPodsByRs(String clusterCode, String perpage, String page, String namesp, String name)
			throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		String urlString = PropertiesUtil.getConfigValueCache("k8s", "getPodsByRs.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode, namesp, name);
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = HttpRequestUtil.target;
		Response response = target.queryParam("perpage", perpage).queryParam("page", page).request().get();

		return requestUtil.getResponseJsonData(response);
	}

	@Override
	public String getPodsByNode(String clusterCode, String nodeip, String perpage, String page) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getPodsByNode.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode, nodeip);
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = HttpRequestUtil.target;
		Response response = target.queryParam("perpage", perpage).queryParam("page", page).request().get();

		return requestUtil.getResponseJsonData(response);
	}

	@Override
	public String getPodDetail(String clusterCode, String namespace, String name) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getPodDetail.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode, namespace, name);
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = HttpRequestUtil.target;
		Response response = target.request().get();

		return requestUtil.getResponseJsonData(response);
	}

	@Override
	public String getPodResource(PodQueryDTO podQueryDTO) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(podQueryDTO.getClusterCode());

		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("podMetric", podQueryDTO.getPodRequestMetricDTOs());

		String urlString = PropertiesUtil.getConfigValueCache("k8s", "getPodResource.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), podQueryDTO.getClusterCode());
		// 创建URL连接
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = HttpRequestUtil.target;
		Response response = target.request().post(Entity.entity(paraMap, MediaType.APPLICATION_JSON_TYPE));

		return requestUtil.getResponseJsonData(response);
	}

}
